package chuichui.game.avatar
{
	import chuichui.Utils.Print;
	import chuichui.Utils.Val;
	import chuichui.game.render.RenderData;

	import flash.display.Sprite;
	import flash.ui.Keyboard;
	import flash.utils.Dictionary;

	public class Avatar
	{
		/**
		 * 当前动作名称
		 */
		public var curAction : int;
		public var scaleX : int;

		/**
		 * 方向只有左右区别
		 * 上下用当前的curkey区分
		 */
		public var dir : int;

		private var avatarId : String;
		public var curActions : Array;
		private var actionDic : Dictionary;
		public var renderList : Array = [];
		private var container : Sprite = new Sprite();

		public function Avatar(avatarId : String, actions : Dictionary)
		{
			this.avatarId = avatarId;
			this.actionDic = actions;

			if(actionDic)
			{
				var render : RenderData;

				for each(var animation : Animation in actionDic[0])
				{
					render = new RenderData();
					render.id = animation.name;
					renderList.push(render);
					container.addChild(render.render);
				}
			}
		}

		public function getContainer() : Sprite
		{
			return container;
		}

		/**
		 * 更换动作,方向
		 * @param action 动作名称
		 * @param dir    方向
		 * @param index  帧数
		 * @param loops  循环次数
		 *
		 */
		public function playAvatar(action : int, dir : int, index : int = 0, loops : int = 0) : void
		{
			curAction = action;

			curActions = actionDic[action];

			if(curActions == null)
			{
				trace("缺少avatar：", avatarId, action);
				return;
			}

			if(dir == Keyboard.RIGHT)
			{
				scaleX = -1;
			}
			else
			{
				scaleX = 1;
			}
			this.dir = dir;
			gotoFrame(index);
		}

		/**
		 * 换方向
		 * @param dir [0-7]
		 * @return
		 */
		public function palyDir(dir : int) : void
		{
			playAvatar(curAction, dir, 0);
		}

		/**
		 * 尝试是否可以播放下一帧
		 * @param elapsedTime
		 * @param duration
		 * @return
		 *
		 */
		private var curFrameIndex : int = 0;

		public function tryNext(elapsedTime : int, duration : int = 0) : void
		{
			if(curActions && curActions.length >= 1)
			{
				var animation : Animation = curActions[0];
				animation.tryNext(elapsedTime, duration);
				curFrameIndex = animation.curFrameIndex;
				this.gotoFrame(curFrameIndex);
			}
		}

		public function updateRender(sceneX : int, sceneY : int, offsetX : int = 0, offsetY : int = 0) : void
		{
			var animation : Animation;
			var render : RenderData;
			var index : int = 0;
			var frame : Frame;

			for each(animation in curActions)
			{
				frame = animation.curFrame;
				render = renderList[index++];

				if(render == null || frame == null || frame.data == null)
				{
					render.bitmapData = null;
					return;
				}
				render.bitmapData = frame.data;
				render.scaleX = scaleX;

				if(scaleX == 1)
				{
					render.x = sceneX + frame.offsetX + offsetX;
				}
				else
				{
					render.x = sceneX - frame.offsetX + offsetX;
				}
				render.y = sceneY + frame.offsetY;
			}

			for(var i : int = index; i < renderList.length; i++)
			{
				render = renderList[i];
				render.bitmapData = null;
			}
		}


		public function gotoFrame(index : int) : void
		{
			var animation : Animation;

			for each(animation in curActions)
			{
				animation.gotoFrame(index);
			}
		}

		public function get isDestroy() : Boolean
		{
			return false;
		}

		public function destroy() : void
		{
			if(container && container.parent)
			{
				container.parent.removeChild(container);
			}
		}


	}
}